package com.ftg.learn.chapter04.linetable;

/**
 * @author 布衣
 */
public class StackForMe02 {

    private class Node{
        private int val;

        private Node next;

        public Node(int val) {
            this.val = val;
        }
        public Node(int val, Node next) {
            this.val = val;
            this.next = next;
        }
    }

    private Node head;

    private int size;

    public void push(int val){
        size++;
        if(head == null){
            head = new Node(val);
            return ;
        }
        head = new Node(val, head);
    }

    public int pop(){
        if(head == null){
            return -1;
        }
        size--;
        Node temp = head;
        head = head.next;
        temp.next = null;
        return temp.val;
    }

    public int peek(){
        if(head == null){
            return -1;
        }
        return head.val;
    }

    public int size(){
        return size;
    }

    public static void main(String[] args) {
        StackForMe02 stackForMe02 = new StackForMe02();
        stackForMe02.push(1);
        stackForMe02.push(2);
        stackForMe02.push(3);

        while (stackForMe02.size > 0){
            System.out.println(stackForMe02.pop());
        }
    }

}
